from mongoengine import DynamicDocument
from mongoengine.fields import StringField
from operation_trajectory.tools import and_operation_trajectory


class BaseModel(DynamicDocument):

    meta = {
        'abstract': True,
    }

    def to_delete(self):
        return self.save()

    def to_new(self):
        return self.to_save()

    def to_save(self):
        instance = self.save()
        return instance


class OperationTrajectoryBaseModel(BaseModel):
    # 仅仅作为抽象类，不作为collection来存储
    meta = {
        'abstract': True,
    }

    @and_operation_trajectory
    def to_delete(self):
        res = super().to_delete()
        return res

    @and_operation_trajectory
    def to_new(self):
        res = super().to_new()
        return res

    @and_operation_trajectory
    def to_update(self):
        res = super().to_save()
        return res


class User(OperationTrajectoryBaseModel):
    meta = {'collection': 'user', 'verbose_name': '用户'}
    username = StringField()
